package com.qianfeng.smartdevice.task;

import com.qianfeng.smartdevice.event.CheckDeviceStatusTaskEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component//交给 spring创建当前对象
public class CheckDeviceStatusTask {
    private ApplicationContext context;
    private CheckDeviceStatusTaskEvent event;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setEvent(CheckDeviceStatusTaskEvent event) {
        this.event = event;
    }

    @Scheduled(fixedRate = 10000)//开启定时任务，每个十秒钟执行一次
    public void checkStatus(){
        System.out.println("检查设备状态的定时任务执行了");
        //检查设备的最后一次时间是不是超出了我们指定的阈值
        //我们没有必要在这个类里面去检查 websocket 的变量,而是可以通过一个事件,直接通知 websocket 让它自己检查,这是单一职责
        context.publishEvent(event);
    }
}
